package com.bbcare.switchData.service.impl;


import com.bbcare.comm.ToolUtils;
import com.bbcare.switchData.dao.oms.QueryUserInfoDao;
import com.bbcare.switchData.entity.model.Account;
import com.bbcare.switchData.entity.model.UserBaseInfo;
import com.bbcare.switchData.service.IQueryUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.bbcare.followup.plat.controller.ChangePasswordController.MD5Encrept;
import static com.bbcare.followup.plat.controller.ChangePasswordController.changeToSHA;


@Service
public class QueryUserInfoServiceImpl implements IQueryUserInfoService {

    @Autowired
    private QueryUserInfoDao queryUserInfoDao;
    @Override
    public List<UserBaseInfo> getUserInfoServiceById(String userId)
    {
        Map<String,Object> param = new HashMap<>();
        param.put("userId",userId);
        param.put("platType",1);
         List<UserBaseInfo> list =  queryUserInfoDao.getUserBaseInfoById(param);
        for (UserBaseInfo item:
             list) {
            if(item.getHeadimg()==null){
                item.setHeadimg("https://small-procedures.oss-cn-hangzhou.aliyuncs.com/yihu/660b668f8c37a135c521bfe063314bd.jpg");
            }
        }
        return list;
    }

    @Override
    public String getUserStatus(String userId) {
        Map<String,Object> param = new HashMap<>();
        param.put("userId",userId);
        return queryUserInfoDao.getUserStatus( param);
    }


    @Override
    public Account doLogin(String phoneNo, String password) {
        Account account = new Account();
        account = queryUserInfoDao.getAccount(phoneNo);
        //用户未设置密码，默认为手机号后6位,验证失败返回null
        if(!(StringUtils.isEmpty(account.getPassword()) && phoneNo.substring(phoneNo.length()-6).equals(password))){
            return account;
        }
        //密码不正确 返回为空
        if((!StringUtils.isEmpty(account.getPassword())&& (Encrept(password)!=account.getPassword()))){
            return account;
        }

        //注入用户信息
        Map paramMap =new HashMap();
        paramMap.put("accountId",account.getId());
        account.userInfos =queryUserInfoDao.getUserBaseInfoById(paramMap);

        return account;
    }
    private static String Encrept(String password) {
        try {
            password = password + "@#_!2018bbcare16888";
            password = changeToSHA(password, "SHA-256");
            password = MD5Encrept(password, 32);
        }catch (Exception e){

        }

        return password;
    }
}
